package com.lancer.dynamic.thread.pool.service;

import com.lancer.dynamic.thread.pool.ThreadPoolManage;
import com.lancer.dynamic.thread.pool.config.ThreadPoolConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

@Component
@Slf4j
public class ThreadPoolServiceImpl {
    private final ThreadPoolManage threadPoolManager;

    public ThreadPoolServiceImpl(ThreadPoolManage threadPoolManager) {
        log.info("初始化线程池服务");
        this.threadPoolManager = threadPoolManager;
    }
    // 根据名称获取线程池
    public ThreadPoolExecutor getThreadPoolByName(String poolName) {
        return threadPoolManager.getThreadPoolByName(poolName);
    }
    // 强制关闭线程池
    public void forceShutdownThreadPool(String poolName) {
        log.info("强制关闭线程池：{}", poolName);
        threadPoolManager.forceShutdownThreadPool(poolName);
    }
    // 关闭线程池
    public void shutdownThreadPool(String poolName) {
        log.info("关闭线程池：{}", poolName);
        threadPoolManager.shutdownThreadPool(poolName);
    }

    // 新增/更新线程池
    public void addOrUpdateThreadPool(ThreadPoolConfig threadPoolConfig) {
        log.info("新增/更新线程池：{}", threadPoolConfig.getPoolName());
        threadPoolManager.addOrUpdateThreadPool(threadPoolConfig);
    }
}
